clear
clear mata
clear matrix

cd ..                                                                            /* Goes back to parent folder */

capture log close
log using "log/12_Agri_Census.log", replace

mkdir /tmp                                                                       /* creates a temporary folder to store temp files */

*       Patriotism!

*       FIRST VERSION  June       7, 2022
*       THIS VERSION   June       9, 2022
*       LAST RUN       June       9, 2022

*       LAST REVISOR		AT, BC 

*       Log of revisions:

*       This prepares variables from 1930 Agricultural Census (ICPSR)

********************************************************************************
****                      PLAN OF THE PROCEDURE                             ****
****                                                                        ****
****                      A. Data on Farmers                                ****
****  1. Prepare raw data  												    ****
****  2. Merge data to correspondence with counties   					    ****
****                      B. Data on farm income                            ****
****  1. Prepare raw data  												    ****
****  2. Merge data to correspondence with counties   					    ****
****                      C. Finalize data                                  ****
****  1. Merge the two databases                     					    ****
****  2. Create variables							  					    ****
****  3. Label and save 							  					    ****
****                      D. Erase junk                                     ****
********************************************************************************

********************************************************************************
****                      A. Data on Farmers                                ****
****  1. Prepare raw data  												    ****
********************************************************************************
do "code-build/01_Shopping_List.do"

use "rawdata/ICPSR/Farmers1930", replace

ren acae001 A30AFARMS
ren acad002 A30AFARMERSO
ren acad003 A30AFARMERSP
ren acad004 A30AFARMERSM
ren acad005 A30AFARMERST

********************************************************************************
****  2. Merge data to correspondence with counties   					    ****
********************************************************************************

ren ICPSRSTI  stateicpsr
ren ICPSRCTYI countyicpsr

merge 1:1 stateicpsr countyicpsr using "rawdata/maps/Bridge", nogen keepus(countynd) keep(mat) /* Not matched from master are AK (4), HI (4) and Yellowstone in Idaho */


collapse (sum)                    A30AFARMS                                                                                               ///
         (count) N_A30AFARMS    = A30AFARMS                                                                                               ///    
		 (sum)                    A30AFARMERSO                  A30AFARMERSP                  A30AFARMERSM                  A30AFARMERST  /// 
		 (count) N_A30AFARMERSO = A30AFARMERSO N_A30AFARMERSP = A30AFARMERSP N_A30AFARMERSM = A30AFARMERSM N_A30AFARMERST = A30AFARMERST, by(stateicpsr countynd)

for VAR in var A30AFARMS A30AFARMERSO A30AFARMERSP A30AFARMERSM A30AFARMERST : replace VAR = . if N_VAR == 0 & VAR != .
drop N_*
		 
save "tmp/farmers", replace
		 
********************************************************************************
****                      B. Data on farm income                            ****
****  1. Prepare raw data  												    ****
********************************************************************************
use "rawdata/ICPSR/35206-0017-Data.dta", clear

keep LEVEL STATE COUNTY VAR2169
keep if LEVEL == 1

ren STATE  stateicpsr
ren COUNTY  countyicpsr40
ren VAR2169 totfarminc29

********************************************************************************
****  2. Merge data to correspondence with counties   					    ****
********************************************************************************
merge 1:m stateicpsr countyicpsr40 using "data/Bridge-countyicpsr1940-countynd", nogen keep(mat)

for VAR in var totfarminc29 : replace VAR = VAR * weight40

collapse (sum) totfarminc29 , by(stateicpsr countynd)

save "tmp/totfarminc29", replace

********************************************************************************
****                      C. Data on cattle                                 ****
****  1. Prepare raw data  												    ****
********************************************************************************
use "rawdata/ICPSR/35206-0013-Data.dta", clear

keep STATE COUNTY STATEFIP FIPS NAME LEVEL VAR192
keep if LEVEL == 1

format FIPS %9.0f
collapse (firstnm) STATEFIP FIPS LEVEL VAR*, by(STATE COUNTY NAME)
replace NAME = proper(NAME)

rename STATE  stateicpsr
rename COUNTY countyicpsr
rename NAME   countyname
ren    VAR192 cattlen30 

********************************************************************************
****  2. Merge data to correspondence with counties   					    ****
********************************************************************************
merge 1:m stateicpsr countyicpsr using rawdata/maps/Bridge, nogen keep(mat)

collapse (sum) cattlen30, by(stateicpsr countynd)

save "tmp/cattle", replace

********************************************************************************
****                      C. Finalize data                                  ****
****  1. Merge the two databases                     					    ****
********************************************************************************
use "tmp/farmers", replace

merge 1:1 stateicpsr countynd using "tmp/totfarminc29", nogen 
merge 1:1 stateicpsr countynd using "tmp/cattle"	  , nogen 

********************************************************************************
****  2. Create variables							  					    ****
********************************************************************************
gen A30FARMERS  = A30AFARMERSO + A30AFARMERSP + A30AFARMERSM + A30AFARMERST

keeporder stateicpsr countynd A30AFARMS A30FARMERS A30AFARMERSO A30AFARMERSP totfarminc29 cattlen30

********************************************************************************
****  3. Label and save 							  					    ****
********************************************************************************

lab var stateicpsr   "State code (ICPSR)"
lab var countynd     "County code (Fishback et al. 2003)"
lab var A30AFARMS    "All land in farms, acres, 1930 (ICPSR)"
lab var A30FARMERS   "Number of farmers of all types (ICPSR)"
lab var A30AFARMERSO "Number of farmer fully owners (ICPSR)" 
lab var A30AFARMERSP "Number of farmer partially owners (ICPSR)" 
lab var totfarminc29 "Total farm products sold, traded, or used by value, 1929, $ (ICPSR)"
lab var cattlen30    "Number of cattle on farms in 1930 (ICPSR)"

save "data/agriC30", replace

********************************************************************************
****  4. Erase junk      							  					    ****
********************************************************************************
erase "tmp/farmers.dta"
erase "tmp/cattle.dta"

rmdir "tmp/"


log close
exit
